package dao;

import java.sql.SQLException;
import java.util.HashMap;

import com.mysql.jdbc.ResultSet;

public class ReverseFileDao {

	private ConnectionDB c ;
	public ReverseFileDao()
	{
		c = new ConnectionDB();
	}
	
	public void insert(int idParaGlobal, String fileName, int indexInFile, int size)
	{
		c.ajouter("Insert into reverseFile values ('"+idParaGlobal +"','" + fileName +"','" + indexInFile + "','" + size + "')");
	}
	
	public String[] getLineByPara(int idPara)
	{
		String[] result = new String[4];
		
		ResultSet rs = c.rechercher("Select * from reverseFile where idParaGlobal = " + idPara + ";");
		
		try {
			while(rs.next())
			{
				result[0] = String.valueOf(rs.getInt(0));
				result[1] = rs.getString(1);
				result[2] = String.valueOf(rs.getInt(3));
				result[3] = String.valueOf(rs.getInt(4));					
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return result;
	}
	
	public HashMap<Integer, String[]> getAll()
	{
		HashMap<Integer, String[]> reverseFile = new HashMap<Integer, String[]>();
		
		ResultSet rs = c.rechercher("Select * from reverseFile ;");
		
		try {
			while(rs.next())
			{
				String[] tmp = new String[3];
				tmp[0] = rs.getString(2);
				tmp[1] = String.valueOf(rs.getInt(3));
				tmp[2] = String.valueOf(rs.getInt(4));	
				int x = rs.getInt(1);
				reverseFile.put(x, tmp);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return reverseFile;
	}
	
	public void close()
	{
		c.fermer();
	}
}
